<?php
/**
 * Classe livraison
 *
 * Description de la classe
 *
 * @author Mathieu
 */
class Delivery extends Object {
    protected $id;
    protected $vendor_id;
    protected $pays_id;
    protected $service;
    protected $cout;
    protected $delai;
    protected $delivery_id;

    function __construct($id="", $params=array()) {
        if($id!='') {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }
    function __toString() {
        if(!isset($this->service)) $this->load();
        return $this->service;
    }

    public function load() {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM livraison WHERE id = '".$this->id."'");
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }

    public function save() {
        $db = Database::getInstance();
        $sql = "INSERT INTO livraison (vendor_id, pays_id, service, cout, delai, delivery_id)
                 VALUES ('".$this->vendor_id."',
                         '".$this->pays_id."',
                         '".$this->service."',
                         '".$this->cout."',
                         '".$this->delai."',
                         '".$this->delivery_id."')";
        $result = $db->query($sql);
    }
    
    public function getCountry() {
        $db = Database::getInstance();
        $result = $db->query("SELECT fr FROM pays WHERE id = '".$this->pays_id."'");
        if(sizeof($result)>0){
            return $result[0]['fr'];
        }
    }

    public function delete(){
        $db = Database::getInstance();
        $result = $db->query("DELETE FROM livraison WHERE id = '".$this->id."' ");
    }
}